package com.guigu.xm.lq.controller;

import com.guigu.xm.advice.Interceptor;
import com.guigu.xm.lq.service.impl.*;
import com.guigu.xm.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
@Controller
@Transactional
@RequestMapping("/suc")
public class SysUsersConller {
    @Autowired(required = false)
    SysUsersServiceImpl sus;
    @Autowired(required = false)
    SysMenusServiceImpl sms;
    @Autowired(required = false)
    SysUserRoleServiceImpl sur;
    @Autowired(required = false)
    SysRolesServiceImpl srs;
    @Autowired(required = false)
    SysMenuRoleServiceImpl smrs;
    @Autowired(required = false)
    SysAuthorizationServiceImpl satrole;
    @Autowired(required = false)
    SysAuthorizationRoleServiceImpl sysRole;
    @Interceptor(methodName = "用户登录")
    @RequestMapping("/login")
    public String index(String username,String password,HttpServletRequest request){
        SysUsers sysUsers = sus.loginSys(username, password);
        List<SysAuthorization> sysAuthorizationRoles = satrole.allRoot(username);
        Map<SysMenus, Map<SysMenus,List<SysMenus>>> xx = sms.xx(username);
        request.getSession().setAttribute("menus",xx);
        request.getSession().setAttribute("login_id",username);
        request.getSession().setAttribute("user",sysUsers);
        request.getSession().setAttribute("allroot",sysAuthorizationRoles);
        if (sysUsers!=null){
            return "/lqym/index.jsp";
        }
        return "";
    }
    /*@Interceptor(methodName = "用户登录")
    @RequestMapping("/login")
    @ResponseBody
    public Map<SysMenus, Map<SysMenus,List<SysMenus>>> index(String username,String password,HttpServletRequest request){
        return sms.xx(username);
    }*/
    public Map<SysMenus,List<SysMenus>> menus(String login_id){
        Map<SysMenus, List<SysMenus>> menuAll = sms.findMenuAll(login_id);
        return menuAll;
    }
    @Interceptor(methodName = "显示全部")
    @PostMapping("/fingAllUesr")
    @ResponseBody
    public PageVo<SysUserRole> findAlluser(@RequestBody Map<String,String> map, HttpServletRequest request){
        String login_id = (String)request.getSession().getAttribute("login_id");
        String name = map.get("name");
        String page = map.get("page");
        String num = map.get("num");
        PageVo<SysUserRole> sysUserRolePageVo = sur.chaUser(login_id, name, page, num);
        return sysUserRolePageVo;
    }
    @PostMapping("/insert")
    @ResponseBody
    public int insert(@RequestBody SysUsers sysUsers){
        SysUsers chaxu = sus.chaxu(sysUsers.getLoginId());
        sysUsers.setStatus("1");
        if(chaxu==null){
            int insert = sus.insert(sysUsers);
            if(insert>0){
                SysUsers chaxu1 = sus.chaxu(sysUsers.getLoginId());
                SysUserRole userRole=new SysUserRole();
                userRole.setUserId(chaxu1.getId());
                userRole.setRoleId(2);
                int insert1 = sur.insert(userRole);
                if(insert1>0){
                    return 1;
                }
            }
        }
        return 0;
    }
    @GetMapping("/selectAllRoles")
    @ResponseBody
    public List<SysRoles> selectAllRoles(HttpServletRequest request){
        String login_id = (String)request.getSession().getAttribute("login_id");
        List<SysRoles> sysRoles = srs.selectAllRoles(login_id);
        return sysRoles;
    }
    @RequestMapping("/update")
    @ResponseBody
    public int updaterole(String id,String descn){
        int i = sur.updateRole(id, descn);
        if(i>0){
            return 1;
        }
        return 0;
    }
    @RequestMapping("/delUser")
    @ResponseBody
    public int delUser(String id){
        int i = sus.DelUser(id);
        if(i>0){
            return 1;
        }
        return 0;
    }
    @Interceptor(methodName = "退出登录")
    @RequestMapping("/out")
    public String Out(HttpServletRequest request)  {
        request.getSession().invalidate();
        return "/login.jsp";
    }
    @RequestMapping("/AddRoles")
    @ResponseBody
    public int  AddRoles(String descn){
        System.out.println(descn);
        SysRoles sysRoles = srs.DaoShu();
        Integer rid = sysRoles.getRid();
        SysRoles addcha = srs.Addcha(descn);
        if(addcha==null){
            if(rid+1<10){
                int rid1=rid+1;
                int i = srs.insert2(rid1, descn);
                if(i>0){
                    return 1;
                }
            }else{
                return 2;
            }
        }
        return 0;
    }
    @GetMapping("/AllRoles")
    @ResponseBody
    public List<SysRoles> AllRoles(){
        List<SysRoles> sysRoles = srs.allRoles();
        return sysRoles;
    }
    @RequestMapping("/AddMenu")
    @ResponseBody
    public int AddMenu(String user_id,String menu_id){
        int i1 = smrs.delMenu(user_id);
            if (!menu_id.equals("")) {
                SysMenuRole sysMenuRole=new SysMenuRole();
                sysMenuRole.setRoleId(Integer.parseInt(user_id));
                String[] split = menu_id.split(",");
                for (int i = 0; i < split.length; i++) {
                    sysMenuRole.setMenuId(Integer.parseInt(split[i]));
                    smrs.insert(sysMenuRole);
                }
                return 1;
            }
        return 0;
    }
    @RequestMapping("/AddQx")
    @ResponseBody
    public int AddQx(String au_id,String role_id){
        System.out.println(au_id+"...."+role_id);
        sysRole.detRoot(Integer.parseInt(role_id));
        if(!au_id.equals("")){
            String[] split = au_id.split(",");
            for (int i=0;i<split.length;i++){
                sysRole.addRoot(split[i],role_id);
            }
            return 1;
        }
        return 0;
    }
    @RequestMapping("/updatepassword")
    @ResponseBody
    public int updatePassword(@RequestBody SysUsers sysUsers){
        int update = sus.updateByPrimaryKeySelective(sysUsers);
        if(update>0){
            return 1;
        }
        return 0;
    }
}
